use chrono;
use log::LevelFilter;
use colored::*; // 引入 colored 库

pub fn init_logger() {
    // 初始化fern日志库
    let now = chrono::Local::now();
    let timestamp = now.format("%Y-%m-%d_%H"); // 格式化时间戳
    let log_filename = format!("output_{}.log", timestamp); // 添加时间戳到文件名

    fern::Dispatch::new()
        .format(move |out, message, record| {
            let level = match record.level() {
                log::Level::Trace => format!("{}", record.level()).bright_magenta(),
                log::Level::Debug => format!("{}", record.level()).bright_blue(),
                log::Level::Info => format!("{}", record.level()).bright_green(),
                log::Level::Warn => format!("{}", record.level()).bright_yellow(),
                log::Level::Error => format!("{}", record.level()).bright_red(),
            };
            out.finish(format_args!(
                "{} [{}] [{}:{}] {}",
                chrono::Local::now().format("%Y-%m-%dT%H:%M:%S"),
                level,
                record.file().unwrap_or("unknown"),
                record.line().unwrap_or(0),
                message
            ))
        })
        .level(LevelFilter::Info)
        .chain(std::io::stdout()) // 将日志输出到stdout
        .chain(fern::log_file(log_filename).unwrap()) // 将日志写入包含时间戳的文件名
        .apply()
        .expect("Failed to initialize logger.");
}
